package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.Menu;
import com.example.mapper.MenuMapper;
import com.example.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hotta
 * @since 2023-05-31
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> getAllMenu() {
        // 查一级
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId, 0);
        List<Menu> list = this.list(wrapper);

        List<Menu> menuList = list.stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("icon", item.getIcon());
            map.put("title", item.getTitle());

            item.setMeta(map);
            return item;
        }).collect(Collectors.toList());

        log.info("------------------------------------------------------------------------",list);
        log.info("{}",list);
        //查二级
        if (menuList != null) {
            for (Menu menu : menuList) {
                LambdaQueryWrapper<Menu> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(Menu::getParentId, menu.getId());
                List<Menu> menus = this.list(wrapper1);

                List<Menu> menuList1 = menus.stream().map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("icon", item.getIcon());
                    map.put("title", item.getTitle());

                    item.setMeta(map);
                    return item;
                }).collect(Collectors.toList());

                menu.setChildren(menuList1);
            }
        }
        return menuList;
    }
}
